package com.itheima.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.book.interceptor.BaseContext;
import com.itheima.book.utils.result.Result;
import com.itheima.book.pojo.entity.Book;
import com.itheima.book.pojo.entity.Carousel;
import com.itheima.book.pojo.entity.User;
import com.itheima.book.service.BookService;
import com.itheima.book.service.CarouselService;
import com.itheima.book.service.FavoriteService;
import com.itheima.book.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/home")
@Api(tags = "主页相关接口")
public class HomeController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private UserService userService;

    /**
     * 查询热门图书
     * @return
     */
    @GetMapping("/books/hot")
    @ApiOperation("查询热门图书")
    public Result gethotbook() {
        log.info("查询热门图书");
        List<Book> bookList = bookService.getHotBook();
        return Result.success(bookList);
    }

    /**
     * 获取轮播图数据
     * @return
     */
    @GetMapping("/carousel/list")
    @ApiOperation("获取轮播图数据")
    public Result getcarousel() {
        log.info("获取轮播图数据");
        List<Carousel> carouselList = carouselService.list();
        return Result.success(carouselList);
    }

    /**
     * 获取书籍详情
     * @param id 书籍ID
     * @return
     */
    @GetMapping("/books/{id}")
    @ApiOperation("获取书籍详情")
    public Result getBookDetail(
            @ApiParam(value = "书籍ID", required = true)
            @PathVariable Long id) {
        log.info("获取书籍详情，id：{}", id);
        Book book = bookService.getById(id);
        if (book != null) {
            return Result.success(book);
        }
        return Result.error("书籍不存在");
    }

    /**
     * 获取书籍收藏状态
     * @param id 书籍ID
     * @return
     */
    @GetMapping("/books/{id}/favorite/status")
    @ApiOperation("获取书籍收藏状态")
    public Result getFavoriteStatus(
            @ApiParam(value = "书籍ID", required = true)
            @PathVariable Long id) {
        log.info("获取书籍收藏状态，id：{}", id);
        Long userId = BaseContext.getCurrentId(); // Temporary hardcoded user ID
        boolean isFavorite = favoriteService.isFavorite(userId, id);
        return Result.success(isFavorite);
    }

    /**
     * 添加收藏
     * @param id 书籍ID
     * @return
     */
    @PostMapping("/books/{id}/favorite/add")
    @ApiOperation("添加收藏")
    public Result addFavorite(
            @ApiParam(value = "书籍ID", required = true)
            @PathVariable Long id) {
        log.info("添加收藏，id：{}", id);
        Long userId = BaseContext.getCurrentId();
        favoriteService.addFavorite(userId, id);
        return Result.success();
    }

    /**
     * 取消收藏
     * @param id 书籍ID
     * @return
     */
    @PostMapping("/books/{id}/favorite/remove")
    @ApiOperation("取消收藏")
    public Result removeFavorite(
            @ApiParam(value = "书籍ID", required = true)
            @PathVariable Long id) {
        log.info("取消收藏，id：{}", id);
        Long userId = BaseContext.getCurrentId();
        favoriteService.removeFavorite(userId, id);
        return Result.success();
    }

    @GetMapping("/user/info")
    @ApiOperation("获取当前用户信息")
    public Result<User> getUserInfo() {
        // 这里使用固定的用户ID 1，实际应该从登录信息中获取
        User user = userService.getById(BaseContext.getCurrentId());
        return Result.success(user);
    }

    @PutMapping("/user/update")
    @ApiOperation("更新用户信息")
    public Result<Boolean> updateUser(@RequestBody User user) {
        // 设置用户ID为1（实际应该从登录信息中获取）
        user.setId(BaseContext.getCurrentId());
        boolean success = userService.updateById(user);
        return success ? Result.success(true) : Result.error("更新失败");
    }
}
